<template>
    <div class="login_page">
        <div class="panel">
            <h1>
                <img :src="logosrc">
            </h1>
            <p class="hint_msg">
                Please enter your account number and enter your password
            </p>
            <el-form :model="form" :rules="rules" class="form">
                <el-form-item prop="username">
                    <i class="icon-ddex-user"></i>
                    <el-input v-model="form.username" auto-complete="off" placeholder="UserName" autofocus></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <i class="icon-ddex-Password"></i>
                    <el-input type="password" v-model="form.password" auto-complete="off" placeholder="PassWord"></el-input>
                </el-form-item>
                <el-form-item prop="validateCode">
                    <i class="icon-ddex-security"></i>
                    <el-row :gutter="10">
                        <el-col :span="16">
                            <el-input type='text' v-model="form.validateCode" auto-complete="off" placeholder="VerifyCode" :maxlength='4'></el-input>
                        </el-col>
                        <el-col :span="8">
                            <div class="codeimg"><img :src="codeImgUrl" @click="codeMethod" alt=""></div>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="loginMethod" :loading="logining" style='width:100%'>Login</el-button>
                </el-form-item>
            </el-form>
            <p class="copyright">
                Copyright@2017 Ultimate Music Co.LTD
            </p>
        </div>
    </div>
</template>
<script>
import { APIs } from '@utils/apis'

export default {
    data() {
        return {
            codeImgUrl: '',
            logosrc: require('@ass/login-logo.png'),
            logining: false,
            form: {
                username: 'admin',
                password: '123456',
                validateCode: ''
            },
            rules: {
                username: [{
                    required: true,
                    trigger: 'blur',
                    validator(rule, value, callback) {
                        if (value.length < 1) {
                            callback(new Error('请输入用户名'))
                            return
                        }
                        if (!/^[a-zA-Z]+.{0,1}[a-zA-Z]+$/.test(value)) {
                            callback(new Error('请输入正确的用户名'));
                            return
                        }
                        callback();
                    }
                }],
                password: [{
                    required: true,
                    trigger: 'blur',
                    validator(rule, value, callback) {
                        if (!value) {
                            callback(new Error('请输入密码'));
                            return
                        }
                        callback();
                    }
                }],
                validateCode: [{
                    required: true,
                    trigger: 'blur',
                    validator(rule, value, callback) {
                        if (value.length < 1) {
                            callback(new Error('请输入验证码'))
                            return
                        }
                        if (!/^[a-zA-Z0-9]{4,5}$/.test(value)) {
                            callback(new Error('请输入正确的验证码'));
                            return
                        }
                        callback();
                    }
                }]
            }
        }
    },

    mounted() {
        this.$nextTick(function() {
            setTimeout(() => {
                this.codeMethod()
            }, 10)
        })
    },

    methods: {
        codeMethod() {
            this.codeImgUrl = `${APIs['API_CODE']}?${Math.random()}`
        },

        loginMethod() {
            this.logining = true
            this.$store.dispatch('loginAction', this.form).then(result => {
                this.$router.push('/home')
                this.logining = false
            })
        },
    }
}
</script>
<style lang="less">
.login_page {
    height: 100%;
    min-width: 1000px;
    overflow: hidden;
    background: url('../assets/ddex-login-bg.png') no-repeat top center;
    background-size: 100% 100%;
    overflow: auto;

    .panel {
        position: fixed;
        width: 350px;
        border-radius: 6px;
        font-size: 13px;
        color: #999999;
        top: 15%;
        left: 50%;
        -webkit-transform: translateX(-50%);
        -moz-transform: translateX(-50%);
        overflow: hidden;
        text-align: center;
        background-color: #F4F7F7;

        h1 {
            width: 100%;
            height: 120px;
            line-height: 130px;
            background-color: #1C89F9;

            img {
                margin-top: 30px;
                width: 180px;
                height: 60px;
            }
        }

        .hint_msg {
            height: 35px;
            line-height: 35px;
            font-size: 11px;
        }

        .form {
            background-color: #fff;
            padding: 25px 45px 20px 45px;

            .el-form-item {
                width: 100%;
                margin-bottom: 18px;
                position: relative;

                i {
                    position: absolute;
                    font-size: 14px;
                    color: #BABABA;
                    top: 11px;
                    left: 12px;
                    z-index: 9999;
                }

                .el-input {
                    font-size: 12px;
                    color: #999999;
                }

                input {
                    height: 33px;
                    border-radius: 6px;
                    color: #999999;
                    padding: 3px 10px 3px 35px;
                }

                .codeimg {
                    height: 32px;
                    width: 100%;
                    border: 1px solid #bfcbd9;
                    border-radius: 6px;
                }

                &:last-child {
                    i {
                        color: #fff;
                        position: static;
                    }
                }
            }

            .el-form-item__error {
                padding-top: 2px;
            }
        }

        .copyright {
            height: 60px;
            line-height: 60px;
            font-size: 11px;
            background-color: #F4F7F7;
            text-align: center;
        }
    }
}
</style>